<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>修改数据Demo</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../assets/libs/layui/css/layui.css" media="all">
</head>

<body>
    <form class="layui-form" lay-filter="example">

        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">影人编号:</label>
            <div class="layui-input-block" style="margin-right: 20px">
                <input type="text" disabled="true" name="performerId" autocomplete="off" id="performerId"
                    class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">影人名称:</label>
            <div class="layui-input-block" style="margin-right: 20px">
                <input type="text" name="performerName" lay-verify="title" autocomplete="off" id="performerName"
                    class="layui-input">
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">影人类型</label>
            <div class="layui-input-block" style="margin-right: 20px">
                <select name="performerType" id='performerType'></select>
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">商家头像:</label>
            <div class="layui-input-block" style="margin-right: 20px">
                <div class="layui-upload-list" id="BtnDel"
                    style="height: 100px; width: 100px;border: 1px solid #cccccc;">
                </div>
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">影人介绍:</label>
            <div class="layui-input-block" style="margin-right: 20px">
                <input type="text" name="performerContent" lay-verify="title" autocomplete="off" id="performerContent"
                    class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" style="text-align: center">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" id="doSave" lay-submit="" lay-filter="updata">修改</button>
                <button type="button" class="layui-btn layui-btn-primary">返回</button>
            </div>
        </div>
    </form>

    <style>
        .imgBox {
            display: flex;
            flex-flow: column;
            justify-content: center;
        }
    </style>
    <script src="../../assets/libs/layui/layui.js" charset="utf-8"></script>
    <script src="../js/util.js" charset="utf-8"></script>
    <script>
        layui.config({
            base: '/module/'
        }).use(['jquery', 'layer', 'table', 'form', 'config', 'laydate', 'upload'], function () {
            var $ = layui.jquery;
            var layer = layui.layer;
            var table = layui.table;
            var form = layui.form;
            var config = layui.config;
            var laydate = layui.laydate;
            var upload = layui.upload;
            var files = null;
            //页面加载时商家使用状态select下拉框的内容
            $.ajax({
                url: config.base_server + 'GetperformerType',
                dataType: 'json',
                type: 'get',
                async: false,
                success: function (data) {
                    data = data.data;
                    $.each(data, function (index, item) {
                        $('#performerType').append(new Option(item.performerName));
                    });
                    form.render("select");
                }
            })

            var req = GetRequest();
            var no = req['editNo'];//获取请求的url参数editNo的值

            upload.render({
                elem: '#BtnDel'
                , auto: false
                , choose: (obj) => {
                    obj.preview((index, file, result) => {
                        let imgUrl = result;
                        files = file;
                        $('#BtnDel').html('<div class="imgBox" ><img src="' + result + '" alt="' + file.name + '" class="layui-upload-img" width="100" height="100"><a class="remove_' + index + ' imgitem  layui-btn layui-btn-xs layui-btn-danger demo-delete">取消</a></div>')
                        $('.remove_' + index).bind('click', function () {
                            files = null;
                            $(this).parent().remove();
                        })
                    });
                }
            });

            //获取当前编辑的数据
            $.ajax({
                url: config.base_server + 'getPerformerById?editNo=' + no,
                dataType: 'json',
                type: 'get',
                success: function (data) {
                    data = data.data;
                    if (!data.length) {
                        return;
                    }
                    $('#performerId').val(data[0].performerId);
                    $('#performerName').val(data[0].performerName);
                    $('#BtnDel').html('<div class="imgBox"><img src="' + data[0].performerImg + '" alt="" class="layui-upload-img" width="100" height="100"></div>')
                    var select = 'dd[lay-value=' + data[0].performerType + ']';
                    $('#performerType').siblings("div.layui-form-select").find('dl').find(select).click();
                    $('#performerContent').val(data[0].performerContent);
                }

            })

            //保存用户数据
            form.on('submit(updata)', function () {
                var data = form.val('example');
                var { performerId, performerName, performerType, performerContent } = data;
                var formData = new FormData();
                formData.append('performerId', performerId);
                formData.append('performerName', performerName);
                formData.append('performerType', performerType);
                formData.append('performerContent', performerContent);
                if (files) {
                    formData.append('performerImg', files)
                }
                layer.load(2);
                $.ajax({
                    type: "POST",
                    url: config.base_server + "updatePerformerById",
                    data: formData,
                    async: false,
                    processData: false,
                    contentType: false,
                    dataType: "json",
                    success: function (data) {
                        if (data.code == 1) {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引 
                            parent.layer.close(index); //再执行关闭
                        }
                    }

                });
            });
        })
    </script>


</body>